package com.sf.wisdomkg.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sf.wisdomkg.R;
import com.sf.wisdomkg.utils.CommonUtils;
import com.sf.wisdomkg.utils.SystemContent;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by ming on 2017/9/22.
 */
public class JianHuRenAddActivity extends AppCompatActivity implements View.OnClickListener{
    private Button back_btn;
    private TextView bar_content;
    private ImageView head_img;

    //相册的操作
    private static final int IMAGE_REQUEST_CODE = 0;
    private static final int CAMERA_REQUEST_CODE = 1;
    private static final int RESULT_REQUEST_CODE = 2;
    private String IMGURL = null;
    /* 照相机缓存头像名称 */
    private static final String IMAGE_FILE_NAME_TEMP = "temp_faceImage.jpg";
    /* 头像名称 */
    private static final String IMAGE_FILE_NAME = "headImage.jpg";
    private Bitmap photo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jianhuren_add);
        getSupportActionBar().hide();
        initView();
        IMGURL = Environment.getExternalStorageDirectory() + "/Android/data/"+ SystemContent.getPkName(JianHuRenAddActivity.this)+"/";
        back_btn.setOnClickListener(this);
        head_img.setOnClickListener(this);
        bar_content.setText("添加监护人");
    }

    /**
     * ui初始化
     */
    private void initView(){
        back_btn = (Button)findViewById(R.id.back_btn);
        bar_content = (TextView)findViewById(R.id.bar_content);
        head_img = (ImageView)findViewById(R.id.head_img);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.back_btn:
                finish();
                break;
            case R.id.head_img:
                CommonUtils.showDialog(JianHuRenAddActivity.this);
                break;
//
        }
    }

    /**
     * 相册操作
     * @param v
     */
    public void on_click(View v) {
        switch (v.getId()) {
            case R.id.openCamera:
                CommonUtils.openCamera();
                break;
            case R.id.openPhones:
                CommonUtils.openPhones();
                break;
            case R.id.cancel:
                CommonUtils.dialog.cancel();
                break;
            default:
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 结果码不等于取消时候
        if (resultCode != RESULT_CANCELED) {

            switch (requestCode) {
                case IMAGE_REQUEST_CODE:// 打开相册返回
                    CommonUtils.startPhotoZoom(data.getData(),JianHuRenAddActivity.this);
                    break;
                case CAMERA_REQUEST_CODE:// 打开相机返回
                    if (CommonUtils.hasSdcard()) {
                        File tempFile = new File(IMGURL + IMAGE_FILE_NAME_TEMP);
                        CommonUtils.startPhotoZoom(Uri.fromFile(tempFile),JianHuRenAddActivity.this);
                    } else {
                        Toast.makeText(getApplicationContext(), "未找到存储卡，无法存储照片！", Toast.LENGTH_LONG)
                                .show();
                    }
                    break;
                case RESULT_REQUEST_CODE:// 裁剪完成,删除照相机缓存的图片
                    final File tempFile = new File(IMGURL + IMAGE_FILE_NAME_TEMP);
                    if (tempFile.exists()) {
                        new Thread() {
                            public void run() {
                                tempFile.delete();
                            };
                        }.start();
                    }

                    // 保存截取后的图片
                    if (data != null) {
                        Bundle extras = data.getExtras();
                        if (extras != null) {
                            photo= extras.getParcelable("data");
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            photo.compress(Bitmap.CompressFormat.PNG, 100, baos);

                            try {
                                File f = new File(IMGURL + IMAGE_FILE_NAME);
                                if (!f.exists()) {
                                    f.createNewFile();
                                }
                                FileOutputStream fOut = new FileOutputStream(f);
                                photo.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                                fOut.flush();
                                fOut.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    if(CommonUtils.dialog !=null){
                        CommonUtils.dialog.cancel();//关闭dialog
                    }

                    break;

            }
        }
    }

}
